
#import "BestLightViewController.h"
#import "CustomNavigationBar.h"
#import "UINavigationItem+Customization.h"
#import "HelpViewController.h"

@implementation BestLightViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    [bgImageView setFrame:self.tableView.bounds];
    self.tableView.backgroundView = bgImageView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setContentInset:UIEdgeInsetsMake(22,0,22,0)];

	NSString *path = [[NSBundle mainBundle] pathForResource:@"BastaLjusetToc" ofType:@"plist"];
	tableOfContents = [NSDictionary dictionaryWithContentsOfFile:path][@"toc"];
//	NSLog(@"TOC: %@", tableOfContents);
//	NSLog(@"count: %d", tableOfContents.count);
    
    UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-off.png"] forState:UIControlStateNormal];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateHighlighted];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateSelected];
    [infoButton sizeToFit];
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *infoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    infoBarButtonItem.accessibilityLabel = @"Hjälp";
    self.navigationItem.rightBarButtonItem = infoBarButtonItem;
    
    [self.navigationItem customizeTitleInNavigationBarWithHeight:self.navigationController.navigationBar.frame.size.height];
    
    documentVC = [[DocumentViewController alloc] initWithNibName:nil bundle:nil];
    for (NSDictionary *item in tableOfContents) {
        [documentVC showDocumentWithName:item[@"document"]];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CustomNavigationBar* customNavigationBar = (CustomNavigationBar*)self.navigationController.navigationBar;
    [customNavigationBar applyDefaultStyle];
}

- (void)showInfo
{
    HelpViewController *helpVC = [[HelpViewController alloc] initWithNibName:nil bundle:nil];
    helpVC.documentFile = _(@"Lighting");
    helpVC.title = _(@"Hjälp");
    [self presentModalViewController:helpVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableOfContents.count;
}

#define kNibName @"BestLightCell"
static CGFloat cellHeight = 0; // Static because height does not change
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!cellHeight) {
        UITableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:kNibName owner:self options:nil][0];
        cellHeight = cell.frame.size.height;
    }
    return cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BestLightCell *cell = (BestLightCell *)[tableView dequeueReusableCellWithIdentifier:kNibName];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:kNibName owner:self options:nil][0];
        cell.delegate = self;
    }
	cell.tag = indexPath.row;
    NSDictionary *item = tableOfContents[indexPath.row];
    cell.titleLabel.text = item[@"title"];
    
	return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - Best light cell delegate
- (void)bestLightCellPressedWithTag:(NSInteger)tag
{
    NSDictionary *item = tableOfContents[tag];
    
    [documentVC showDocumentWithName:item[@"document"]];
    documentVC.backButtonTitle = self.navigationItem.title;
    documentVC.title = item[@"title"];
    [self.navigationController pushViewController:documentVC animated:YES];
}

@end
